package bueu.bexl;

public abstract class BinayExpr extends BexlExpr {

	private final BexlExpr expr1;
	private final BexlExpr expr2;
	
	public BinayExpr(final BexlExpr expr1, final BexlExpr expr2) {
		super(expr1.isRelative() || expr2.isRelative());
		this.expr1 = expr1;
		this.expr2 = expr2;
	}

	public BexlExpr getExpr1() {
		return this.expr1;
	}

	public BexlExpr getExpr2() {
		return this.expr2;
	}

	@Override
	protected Object eval(final BexlCtx ctx, final Object current, final boolean set,
			final Object newValue) throws BexlException {
		return evaluate(this.expr1.eval(ctx, current, set, newValue),
				this.expr2.eval(ctx, current, set, newValue));
	}
	
	protected abstract Object evaluate(Object value1, Object value2);
	
	protected final String toString(final String separator) {
		StringBuilder sb = new StringBuilder("(");
		sb.append(this.expr1);
		sb.append(' ').append(separator).append(' ');
		sb.append(this.expr2).append(')');

		return sb.toString();
	}
}
